#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Joycon 连接检查脚本
"""

import subprocess
import sys

def check_bluetooth_devices():
    """检查蓝牙设备"""
    print("检查蓝牙设备...")
    
    try:
        result = subprocess.run(['bluetoothctl', 'devices'], 
                              capture_output=True, text=True, timeout=10)
        
        joycon_found = False
        for line in result.stdout.split('\n'):
            if 'joy-con' in line.lower() or 'nintendo' in line.lower():
                print(f"发现 Joycon: {line.strip()}")
                joycon_found = True
        
        if not joycon_found:
            print("未发现 Joycon 设备")
        
        return joycon_found
        
    except Exception as e:
        print(f"检查失败: {e}")
        return False

def check_pygame_devices():
    """检查 pygame 设备"""
    print("检查 pygame 设备...")
    
    try:
        import pygame
        pygame.init()
        pygame.joystick.init()
        
        count = pygame.joystick.get_count()
        print(f"检测到 {count} 个手柄")
        
        for i in range(count):
            joy = pygame.joystick.Joystick(i)
            joy.init()
            print(f"手柄 {i}: {joy.get_name()}")
        
        pygame.quit()
        return count > 0
        
    except Exception as e:
        print(f"检查失败: {e}")
        return False

def main():
    print("Joycon 连接诊断")
    print("="*30)
    
    bluetooth_ok = check_bluetooth_devices()
    pygame_ok = check_pygame_devices()
    
    print("\n总结:")
    if bluetooth_ok and pygame_ok:
        print("✅ 连接正常")
    else:
        print("❌ 连接有问题")
        print("建议:")
        print("1. 确保 Joycon 已配对")
        print("2. 尝试重新配对")
        print("3. 运行: sudo modprobe hid-nintendo")

if __name__ == "__main__":
    main() 